package com.smartwebx.ha.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;

import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpProtocolException;
public class FTPUtil {
	/** 
	 * 连接ftp服务器 JDK 1.7 
	 */
	public static FtpClient connectFTP(String url, int port, String username, String password) { //创建ftp	
		FtpClient ftp=null;
		try {
			SocketAddress addr=new InetSocketAddress(url,port);
			ftp=FtpClient.create();ftp.connect(addr);
			ftp.login(username,password.toCharArray());
			ftp.setBinaryType();
			System.out.println(ftp.getWelcomeMsg());
		}catch (FtpProtocolException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
		return ftp;
	}

	public static void changeDirectory(FtpClient ftp,String path) {
		try {ftp.changeDirectory(path);
		System.out.println(ftp.getWorkingDirectory());
		}catch (FtpProtocolException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void disconnectFTP(FtpClient ftp){
		try {
			ftp.close();
			System.out.println("disconnect success!!");
		}catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void upload(String localFile,String ftpFile,FtpClient ftp){
		OutputStream os=null;
		FileInputStream fis=null;
		try {
			os=ftp.putFileStream(ftpFile);
			File file=new File(localFile);
			fis=new FileInputStream(file);
			byte[] bytes=new byte[1024];
			int c;
			while((c=fis.read(bytes))!=-1){
				os.write(bytes,0,c);
			}
			System.out.println("upload success!!");
		}catch (
				FtpProtocolException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(os!=null)	
					os.close();
				if(fis!=null)	fis.close();
			}catch (IOException e) {
				// TODO Auto-generated catch block	
				e.printStackTrace();
			}
		}
	}


	public static void download(String localFile,String ftpFile,FtpClient ftp){
		InputStream is=null;
		FileOutputStream fos=null;
		try {
			is=ftp.getFileStream(ftpFile);
			File file=new File(localFile);
			byte[] bytes=new byte[1024];
			int i;
			fos=new FileOutputStream(file);
			while((i=is.read(bytes))!=-1){
				fos.write(bytes, 0, i);
			}
			System.out.println("download success!!");
		}catch (FtpProtocolException e) {
			// TODO Auto-generated catch block	
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block	
			e.printStackTrace();
		}finally{
			try {if(fos!=null)	fos.close();
			if(is!=null){is.close();
			}}catch (IOException e) {
				// TODO Auto-generated catch block	
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		try {
			InetAddress addr=InetAddress.getLocalHost();
			System.out.println(addr.getHostAddress());
		}catch (UnknownHostException e) {
			// TODO Auto-generated catch block	
			e.printStackTrace();
		}
		String ip="davidiao.gotoftp5.com";
		int port=21;
		String username="davidiao";
		String password="15q15e69T03418";
		String path="/wwwroot";
		FtpClient ftp=FTPUtil.connectFTP(ip, port, username, password);
		System.out.println(ftp.getWelcomeMsg());
		FTPUtil.changeDirectory(ftp, path);
		System.out.println("-----上传----");
		FTPUtil.upload("D:\\test.kml", "/wwwroot/js/aaa.kml", ftp);
		//System.out.println("-----下载----");
		//FTPUtil.download("D:\\aaa.txt", "/home/aaa.txt", ftp);
	}
}